#ifndef __JSON_ANIMATION_H__
#define __JSON_ANIMATION_H__
#include <vector>
#include <string>
#include <map>
#include "Common/singleton.h"

namespace JsAnimation {
using namespace std;

struct Normal2DData {
	int m_x;
	int m_y;
	int m_alpha;
};

// everything key data load from json
// not used json's config directly, better understanding than it used in GameStates
// but less flexible than it.
struct Normal2DKey {
	float m_keyTime;
	int m_x;
	int m_y;
	int m_alpha;
};

#define DT_LIMITED MAX_FLOAT;

class JsonAnimation {
public:
	typedef vector<Normal2DKey> KeyDataVec_t;
	typedef KeyDataVec_t::iterator KeyDataVecIter_t;

	JsonAnimation();

	~JsonAnimation() {
	}

	void Update(float dt);
	
	// temporary use it, Need copy data when saving
	const Normal2DData* GetCurrentData() 
	{
		if( !m_isValid ) 
		{
			Normal2DKey& keyData = m_keyDatas.front();
			m_currentData.m_x = keyData.m_x;
			m_currentData.m_y = keyData.m_y;
			m_currentData.m_alpha = keyData.m_alpha;
			m_isValid = true;
		}

		return &m_currentData;
	}

	void AddKeyData(const Normal2DKey& data) {
		m_keyDatas.push_back(data);
	}

	void Pause() {
		m_isPause = true;
	}

	void Resume() {
		m_isPause = false;
	}

	bool IsAnimationEnd() {
		return m_isEnd;
	}

	void SetSpeed(float speed)
	{
		m_speed = speed; 
	}

	float GetSpeed()
	{
		return m_speed;
	}

private:
	KeyDataVec_t m_keyDatas;
	Normal2DData m_currentData;
	float m_curAnimTime;
	bool m_isValid;
	bool m_isPause;
	bool m_isEnd;
	float m_speed;
	static const float FLOAT_DES;
};


class JsonAnimationGroup {
public:
	typedef std::map<std::string, JsonAnimation*> AnimMap_t;
	typedef AnimMap_t::iterator AnimMapIter_t;

	JsonAnimationGroup() 
	{
		m_anims.clear();
		m_isPause = false;
	}

	~JsonAnimationGroup();

	void Update(float dt);

	bool LoadFromConfig(const char* filename);

	JsonAnimation* GetAnimation(const char* animName);

	void Pause() {
		m_isPause = true;
	}

	void Resume() {
		m_isPause = false;
	}

	void SetSpeed( float speed);
private:

	AnimMap_t m_anims;

	bool m_isPause;

};


// design Non-intrusive animation, just offer the data
// not good for OO but do the least change in current code and easy to combine
class JsonAnimationManager : public Singleton<JsonAnimationManager>{
public:
	JsonAnimationManager() {
	}

	~JsonAnimationManager() {
	}

	void Update(float dt);

	JsonAnimation* GetAnimation(const char* animGroupName, const char* animName);
	JsonAnimationGroup* GetAnimationGroup(const char* animGroupName);
	// temporary use it, Need copy data when saving
	const Normal2DData* GetAnimationData(const char* animGroupName, const char* animName);
	bool IsAnimationEnd(const char* animGroupName, const char* animName);
	bool CreateNewAnimationGroup(const char* filename);

private:
	typedef std::map<std::string, JsonAnimationGroup*> AnimGroupMap_t;
	typedef AnimGroupMap_t::iterator AnimGroupMapIter_t;
	AnimGroupMap_t m_animGroups;
};





}
#endif//__JSON_ANIMATION_H__